#!/usr/bin/env python 
# coding:utf-8
# @Time :9/28/18 11:36

import copy
import json
import sys

import click

sys.path.append("..")
sys.path.append("../..")
sys.path.append("../../..")

from proxy.proxy_type import ProxyType
from common.date_util import DateUtil
from ext.task_base import TaskBase
from logger import AppLogger
from mq.mq_factory import MQFactory

logger = AppLogger('weifang_new_register.log').get_logger()


class WeiFangNewRegister(TaskBase):
    __START_URL = "http://www.wfcredit.gov.cn/many/querySgs"

    # 新gsxt的线索队列
    __GSXT_DATA = {
        "province": u"山东",
        "company": None,
        "source_url": u"www.wfcredit.gov.cn",
        "register_code": None,
    }

    __FORMAT_DEMO = {'count': '10', 'ouid': '0', 'ckType': '1', 'page': '1', 'mc': '0'}

    def __init__(self, before_date, log):
        super(WeiFangNewRegister, self).__init__(log)
        self._reset_beanstalk_handler(MQFactory.get_gsxt_clue_beanstalk_handler(log))
        self.__before_date = DateUtil.get_before_day(before_date)
        self.log.info("获得 {} 之后的数据...".format(self.__before_date))
        self.__set_headers()

    def __set_headers(self):
        headers = {
            "Host": "www.wfcredit.gov.cn",
            "Connection": "keep-alive",
            "Origin": "http://www.wfcredit.gov.cn",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7,ja;q=0.6",
        }
        self._proxy_requests.set_header(headers)

    def __get_page_data(self, page):
        form_data = copy.deepcopy(self.__FORMAT_DEMO)
        form_data["page"] = page
        resp = self._proxy_requests.post(self.__START_URL, proxy_type=ProxyType.KUNPENG_DYNAMIC,
                                         data=form_data)

        if resp is None:
            return -1

        # json_resp = None
        try:
            json_resp = json.loads(resp.content)
        except BaseException as e:
            self.log.error("__get_page_data 解析json数据时发生错误...")
            self.log.exception(e)
            return -1

        company_list = json_resp.get("list")

        if not company_list:
            self.log.warn("__get_page_data 未正确获得 company_list 数据， 此时page为={}".format(page))
            return 0

        for per_company in company_list:
            # print json.dumps(per_company, ensure_ascii=False)
            company_name = per_company.get("xdr")
            register_code = per_company.get("xdrshxym")
            date = per_company.get("jdrq")
            if not isinstance(date, basestring):
                self.log.warn("__get_page_data 获取日期数据异常，请检查...")
                continue
            date = date.replace("/", "-")
            print date, self.__before_date
            if date < self.__before_date:
                return -2

            gsxt_data = copy.deepcopy(self.__GSXT_DATA)
            gsxt_data["company"] = company_name
            gsxt_data["register_code"] = register_code
            self.log.info(json.dumps(gsxt_data, ensure_ascii=False))
            self.push_gsxt_clue_data(gsxt_data)

        total_page = json_resp.get("pageCount")

        if not isinstance(total_page, int) or (total_page < 0):
            self.log.warn("__get_page_data 获取总页面信息时发生错误...")
            return -1
        return total_page

    def start(self, *args, **kwargs):
        total_page = self.__get_page_data(1)
        print total_page
        if total_page <= 0:
            self.log.warn("获取页码数信息异常，不进行遍历: total_page = {}".format(total_page))
            return

        for page in xrange(2, total_page + 1):
            try:
                self.log.info("当前采集页面: page = {}".format(page))
                result = self.__get_page_data(page)
                if result == -2:
                    self.log.info("采到时间截止，停止采集..")
                    break
                self.log.info("当前页面采集完成: page = {}".format(page))
            except Exception as e:
                self.log.error("当前页面采集失败: page = {}".format(page))
                self.log.exception(e)

        self.log.info("成功退出采集程序...")


@click.command()
@click.option('--before_date',
              default=20,
              type=int,
              help='采集截止时间')
def main(before_date):
    try:
        WeiFangNewRegister(before_date, logger)()
    except Exception as e:
        logger.error("采集异常退出: ")
        logger.exception(e)


if __name__ == '__main__':
    main()
